package com.foreveross.bsl.mam.app.application.impl;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.Iterator;
import java.util.List;

import javax.inject.Inject;
import javax.inject.Named;

import org.apache.commons.collections.CollectionUtils;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageImpl;
import org.springframework.data.domain.Pageable;

import com.foreveross.bsl.common.utils.bean.BeanHelper;
import com.foreveross.bsl.common.utils.enums.OperatEnum;
import com.foreveross.bsl.mam.app.application.AppCategoryApplication;
import com.foreveross.bsl.mam.app.application.AppMainApplication;
import com.foreveross.bsl.mam.app.application.AppVersionApplication;
import com.foreveross.bsl.mam.app.application.vo.AppCategoryVo;
import com.foreveross.bsl.mam.app.application.vo.AppMainVo;
import com.foreveross.bsl.mam.model.app.domain.AppMain;
import com.foreveross.bsl.system.application.ApplicationRegisterApplication;

/**
 * @author tanzhixiong
 * @2013-6-22 @上午10:53:36
 */
@Named("appMainApplication")
public class AppMainApplicationImpl implements AppMainApplication {

	// @Inject private RoleApplication roleApplication;
	// @Inject private RbacUserRoleApplication rbacApplication;

	@Inject
	private AppCategoryApplication appCategoryApplication;
	@Inject
	private AppVersionApplication appVersionApplication;
	@Inject
	private ApplicationRegisterApplication applicationRegisterApplication;

	/*
	 * (non-Javadoc) modify by heyaowen 20141029 针对ws
	 *
	 * @see
	 * com.foreveross.bsl.mam.app.application.AppMainApplication#findContactAppMains
	 * (java.lang.String)
	 */
	@SuppressWarnings("unchecked")
	@Override
	public List<AppMainVo> findContactAppMains(String identifier, String platform) {
		List<AppMainVo> appMains = new ArrayList<AppMainVo>();
		AppMain appMain = new AppMain();
		AppMainVo appMainVo;
		List<AppMain> appMainlsit = appMain.findListByProperty("identifier", OperatEnum.EQ, identifier);
		if(appMainlsit == null || appMainlsit.size() == 0)
			return appMains;
		appMain = appMainlsit.get(0);
		AppMainVo appVo = new AppMainVo();
		BeanHelper.copyProperties(appVo, appMain);
		appVo.setAppKey(this.applicationRegisterApplication.getAppKeyByAppId(appMain.getId())); // 获取注册应用时生成的appkey
		appVo.setAppCategoryVo(appCategoryApplication.getEntity(appMain.getCategoryid()));
		appVo.setAppVersionlist(this.appVersionApplication.getAppStatus_platform(appMain.getId(), 4, platform)); // 包含最新版本
		// 关联自己本身
		appMains.add(appVo);
		if(appMain.getAppIds() == null) {
			return appMains;
		}
		for (String oAppId : appMain.getAppIds()) {
			appMainVo = new AppMainVo();
			AppMain oAppMain = (AppMain) appMain.findOne(oAppId);
			BeanHelper.copyProperties(appMainVo, oAppMain); // cp属性
			appMainVo.setAppKey(this.applicationRegisterApplication.getAppKeyByAppId(oAppId)); // 获取注册应用时生成的appkey
			appMainVo.setAppCategoryVo(this.appCategoryApplication.getEntity(oAppMain.getCategoryid()));
			// 4 代表已上架的应用 就是最新应用 根据平台信息区别
			// appMainVo.setAppVersionlist(this.appVersionApplication.getAppStatus_platform(oAppMain.getId(), 4, platform));
			appMainVo.setAppVersionlist(this.appVersionApplication.findByPlatform(platform, oAppMain.getId())); // 此平台上所有版本
			appMains.add(appMainVo);
		}
		return appMains;
	}

	/*
	 * (non-Javadoc) modify by heyaowen 20141029
	 *
	 * @see
	 * com.foreveross.bsl.mam.app.application.AppMainApplication#findApp(java
	 * .lang.String)
	 */
	@Override
	public AppMainVo findApp(String appId) {
		AppMain appMain = new AppMain();
		appMain = (AppMain) appMain.findOne(appId);
		AppMainVo appMainVo = new AppMainVo();
		BeanHelper.copyProperties(appMainVo, appMain);
		AppCategoryVo cavo = this.appCategoryApplication.getEntity(appMain.getCategoryid());
		appMainVo.setAppCategoryVo(cavo);
		// 设置所有关联的app
		List<AppMainVo> appVos = new ArrayList<AppMainVo>();
		if (appMain.getAppIds() == null) { // 默认没有关联
			return appMainVo;
		}
		for (String id : appMain.getAppIds()) {
			appVos.add(this.getEntity(id));
		}
		appMainVo.setAppMains(appVos);
		return appMainVo;
	}

	/*
	 * (non-Javadoc) modify by heyaowen 20141029
	 *
	 * @see
	 * com.foreveross.bsl.mam.app.application.AppMainApplication#addApps(
	 * java.lang.String, List<java.lang.String>)
	 */
	@Override
	public void addApps(String appId,  List<String> ids) {
		System.out.println("添加应用关联...");
		AppMain appMain = new AppMain();
		appMain = (AppMain) appMain.findOne(appId);
		if(appMain.getAppIds() == null) {
			appMain.setAppIds(ids.toArray(new String[0]));
			appMain.save();
			return;
		}
		List<String> aIds = Arrays.asList(appMain.getAppIds());
		Collection<String> union = CollectionUtils.union(aIds, ids);
		appMain.setAppIds(union.toArray(new String[0]));
		appMain.save();
	}

	/*
	 * (non-Javadoc) modify by heyaowen 20141029
	 *
	 * @see
	 * com.foreveross.bsl.mam.app.application.AppMainApplication#delApps(
	 * java.lang.String, List<java.lang.String>)
	 */
	@Override
	public void delApps(String appId, List<String> ids) {
		System.out.println("删除关联应用..." + appId);
		AppMain appMain = new AppMain();
		appMain = (AppMain) appMain.findOne(appId);
		List<String> aIds = Arrays.asList(appMain.getAppIds());
		Collection<String> subtract = CollectionUtils.subtract(aIds, ids);
		appMain.setAppIds(subtract.toArray(new String[0]));
		appMain.save();
	}

	/*public static void main(String[] args) {
		String[] a = {"1", "4", "3", "6"};
		String[] b = { "2", "5"};
		List<String> aIds = Arrays.asList(a);
		List<String> sIds = Arrays.asList(b);
		Collection<String> subtract = CollectionUtils.union(aIds, sIds);
		System.out.println(ArrayUtils.toString(subtract.toArray()));
	}*/

	public AppMainVo saveEntity(AppMainVo entity) {
		AppMain appMain = new AppMain();
		BeanHelper.copyProperties(appMain, entity);
		AppMain app = (AppMain) appMain.save();
		entity.setId(app.getId());
		/*
		 * //创建应用角色 RoleVO role =
		 * roleApplication.getRoleByRoleName(app.getIdentifier()); if(role ==
		 * null){ RoleVO roleVO = new RoleVO();
		 * roleVO.setRolename(app.getIdentifier());
		 * roleVO.setName(app.getApp_name());
		 * roleVO.setRoleType(RoleVO.RoleType.APP);//把角色设置为应用角色
		 * roleApplication.saveEntity(roleVO); }
		 */
		return entity;
	}

	public AppMainVo updateEntity(String id, AppMainVo entity) {
		AppMain appMain = new AppMain();
		appMain = (AppMain) appMain.findOne(id);
		BeanHelper.copyProperties(appMain, entity);
		appMain.save();
		BeanHelper.copyProperties(entity, appMain);
		return entity;
	}

	public void deleteEntity(String id) {
		AppMain appMain = new AppMain();
		appMain = (AppMain) appMain.findOne(id);
		appMain.delete();
		/*
		 * // 删除应用时删除相应的角色 RoleVO role = roleApplication
		 * .getRoleByRoleName(appMain.getIdentifier()); if (null != role) { //
		 * 删除授权 rbacApplication.deleteByRoleId(role.getId()); // 删除角色
		 * roleApplication.deleteEntity(role); }
		 */
	}

	public AppMainVo getEntity(String id) {
		AppMain appMain = new AppMain();
		appMain = (AppMain) appMain.findOne(id);
		AppMainVo appMainVo = new AppMainVo();
		BeanHelper.copyProperties(appMainVo, appMain);
		AppCategoryVo cavo = appCategoryApplication.getEntity(appMain
				.getCategoryid());
		appMainVo.setAppCategoryVo(cavo);
		return appMainVo;
	}

	@SuppressWarnings("rawtypes")
	public List<AppMainVo> findAll() {
		AppMain appMain = new AppMain();
		Iterable iterable = appMain.findAll();
		if (iterable != null) {
			List<AppMainVo> result = new ArrayList<AppMainVo>();
			AppMainVo vo;
			for (Iterator iterator = iterable.iterator(); iterator.hasNext();) {
				AppMain obj = (AppMain) iterator.next();
				if (obj.getCategoryid() != null) {
					AppCategoryVo cavo = appCategoryApplication.getEntity(obj.getCategoryid());
					if (cavo != null) {
						vo = new AppMainVo();
						BeanHelper.copyProperties(vo, obj);
						result.add(vo);
					}
				}
			}
			return result;
		}
		return null;
	}

	@SuppressWarnings("rawtypes")
	public Page<AppMainVo> findByPageable(Pageable pageable) {
		AppMain appMain = new AppMain();
		Page page = appMain.findAll(pageable);
		if (page != null && page.getContent() != null) {
			List<AppMainVo> result = new ArrayList<AppMainVo>();
			AppMainVo vo;
			for (Iterator iterator = page.getContent().iterator(); iterator
					.hasNext();) {
				AppMain obj = (AppMain) iterator.next();

				vo = new AppMainVo();
				BeanHelper.copyProperties(vo, obj);
				result.add(vo);
			}
			Page<AppMainVo> pageVO = new PageImpl<AppMainVo>(result, pageable,
					page.getTotalElements());
			return pageVO;
		}
		return null;
	}

	@SuppressWarnings("unchecked")
	public List<AppMainVo> getEntitybyCategoryId(String id) {

		AppMain appMain = new AppMain();

		List<AppMain> appMainlsit = appMain.findListByProperty("categoryid",
				OperatEnum.EQ, id);
		if (appMainlsit != null) {
			List<AppMainVo> result = new ArrayList<AppMainVo>();
			AppMainVo vo;
			for (int i = 0; i < appMainlsit.size(); i++) {
				vo = new AppMainVo();
				BeanHelper.copyProperties(vo, appMainlsit.get(i));
				result.add(vo);
			}
			return result;
		}

		return null;
	}

	@SuppressWarnings("unchecked")
	@Override
	public AppMainVo getEntitybyIdentify(String identifyer) {
		AppMain appMain = new AppMain();
		List<AppMain> appMainlsit = appMain.findListByProperty("identifier",
				OperatEnum.EQ, identifyer);
		if (appMainlsit != null && appMainlsit.size() > 0) {
			AppMainVo vo = new AppMainVo();
			BeanHelper.copyProperties(vo, appMainlsit.get(0));
			return vo;
		}
		return null;
	}

	@SuppressWarnings("unchecked")
	@Override
	public AppMainVo checkName(String name) {
		AppMain appMain = new AppMain();
		List<AppMain> appMainlsit = appMain.findListByProperty("app_name",
				OperatEnum.EQ, name);
		if (appMainlsit != null && appMainlsit.size() > 0) {
			AppMainVo vo = new AppMainVo();
			BeanHelper.copyProperties(vo, appMainlsit.get(0));
			return vo;
		}
		return null;
	}
}
